// pages/teacher/qingjia/qingjia.js
Page({
  data: {
    statusFilters: ['全部', '待审批', '已批准', '已拒绝'],
    statusFilterIndex: 0,
    courseFilters: [
      { id: 0, name: '全部课程' },
      { id: 1, name: '高等数学' },
      { id: 2, name: '线性代数' },
      { id: 3, name: '数据结构' }
    ],
    courseFilterIndex: 0,
    leaveList: [],
    leaveStats: {
      total: 0,
      pending: 0,
      approved: 0,
      rejected: 0
    },
    isLoading: false
  },

  onLoad: function() {
    this.fetchLeaveList();
  },
  
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchLeaveList();
  },
  
  // 状态筛选变更
  changeStatusFilter: function(e) {
    this.setData({
      statusFilterIndex: e.detail.value
    });
    
    this.fetchLeaveList();
  },
  
  // 课程筛选变更
  changeCourseFilter: function(e) {
    this.setData({
      courseFilterIndex: e.detail.value
    });
    
    this.fetchLeaveList();
  },
  
  // 获取请假列表
  fetchLeaveList: function() {
    this.setData({ isLoading: true });
    
    const status = this.data.statusFilters[this.data.statusFilterIndex];
    const courseId = this.data.courseFilters[this.data.courseFilterIndex].id;
    
    // 模拟请求
    setTimeout(() => {
      // 模拟数据
      const mockLeaves = [
        { 
          id: 1, 
          studentName: '张三', 
          studentId: '2023001', 
          type: '事假', 
          startTime: '2023-12-15', 
          endTime: '2023-12-16', 
          days: 2, 
          reason: '家中有事', 
          status: '待审批',
          applyTime: '2023-12-14 10:25',
          courseId: 1
        },
        { 
          id: 2, 
          studentName: '李四', 
          studentId: '2023002', 
          type: '病假', 
          startTime: '2023-12-16', 
          endTime: '2023-12-18', 
          days: 3, 
          reason: '感冒发烧', 
          status: '已批准',
          applyTime: '2023-12-15 08:30',
          approver: '王主任',
          courseId: 1
        },
        { 
          id: 3, 
          studentName: '王五', 
          studentId: '2023003', 
          type: '事假', 
          startTime: '2023-12-17', 
          endTime: '2023-12-17', 
          days: 1, 
          reason: '办理银行卡', 
          status: '已拒绝',
          applyTime: '2023-12-16 15:40',
          approver: '李主任',
          courseId: 2
        },
        { 
          id: 4, 
          studentName: '赵六', 
          studentId: '2023004', 
          type: '病假', 
          startTime: '2023-12-18', 
          endTime: '2023-12-20', 
          days: 3, 
          reason: '发烧住院', 
          status: '待审批',
          applyTime: '2023-12-17 09:15',
          courseId: 3
        }
      ];
      
      // 根据筛选条件过滤
      let filteredLeaves = [...mockLeaves];
      
      if (status !== '全部') {
        filteredLeaves = filteredLeaves.filter(item => item.status === status);
      }
      
      if (courseId !== 0) {
        filteredLeaves = filteredLeaves.filter(item => item.courseId === courseId);
      }
      
      // 计算统计数据
      const stats = {
        total: mockLeaves.length,
        pending: mockLeaves.filter(item => item.status === '待审批').length,
        approved: mockLeaves.filter(item => item.status === '已批准').length,
        rejected: mockLeaves.filter(item => item.status === '已拒绝').length
      };
      
      this.setData({
        leaveList: filteredLeaves,
        leaveStats: stats,
        isLoading: false
      });
    }, 500);
  },
  
  // 查看请假详情
  viewLeaveDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/teacher/qingjia/detail/detail?id=${id}`
    });
  }
});